import path from 'path'
import { RequestMethods } from '../interface/typescript'
import fs from 'fs'
import { DataType } from '../interface/swagger'
export const getFileType = (filePath: string) => path.extname(filePath)

export function getRequestMethod(apiName: string): RequestMethods {
  return /post/.test(apiName.toLowerCase()) ? RequestMethods.post : RequestMethods.get
}
export const getFileNameByLine = (fileName: string) => fileName.split('-')[0]

/**
 *
 * @returns 去除空格和符号-/ << >>
 */
export const clearNoUseSign = (str: string) => str.replace(/[^a-zA-Z0-9\u4e00-\u9fa5\s]/g, '')

/**
 * 获取普通值的类型映射 interage, string, array
 * @param type
 * @returns
 */

/**
 * get method name by path like '/gameCenter/redEnvelope/updateRedEnvelopeActivityQuery'
 * it will return the last of sign '/' word combination like updateRedEnvelopeActivityQuery
 * 通过请求路径定义请求方法名
 * @param path
 */
export const getRequestNameByPath = (path: string) => path.replace(/\//g, '')

export const getOnlyEngilsh = (str: string) => str.replace(/[^a-zA-Z]/g, '')

export const upCaseFitstChat = (str: string) => str.charAt(0).toUpperCase() + str.slice(1)

export const removeDefine = (str: string) =>
  str.match(/^#\/definitions\/(.*)/) && str.match(/^#\/definitions\/(.*)/)!.length > 0
    ? str.match(/^#\/definitions\/(.*)/)![1]
    : str

export const removeComponenStr = (str: string) =>
  str.match(/^#\/components\/schemas\/(.*)$/) !== null
    ? str.match(/^#\/components\/schemas\/(.*)$/)![1]
    : str

export const removeDoubbleRow = (str: string) => str.replace('«', '').replace('»', '')

export const hasKey = (object: object, key: string) =>typeof object === 'object' && object !== null && object.hasOwnProperty(key)

export const getFileNameByTags = (str: string) =>
  str.replace(/-([a-z])/g, (match, p1) => p1.toUpperCase())
export const removeDuplicate = (arr: Array<any>) => [...new Set(arr)]

//找出两个数组的不同
export const removeDuplicateArr = (arr1: Array<any>, arr2: Array<any>) => {
  // 通过set的对称差值
  let set1 = new Set(arr1)
  let set2 = new Set(arr2)
  return [...arr1.filter((x) => !set2.has(x)), ...arr2.filter((y) => !set1.has(y))]
}

export const removeBreakNblank = (str: string) => str? str.replace(/\ +/g, '').replace(/[\r\n]/g, ''): undefined

export const TranslationNormalType = (type: string) =>
  type === DataType.String
    ? 'string'
    : type === DataType.Number
    ? 'number'
    : type === DataType.Boolen
    ? 'boolen'
    : type

export const isNormalType = (type: string) =>
  type === DataType.Boolen ||
  type === DataType.String ||
  type === DataType.Number ||
  type === 'number'
export const TranslationPropType = (type: string) => {
  return type === DataType.String
    ? 'string'
    : type === DataType.Number
    ? 'number'
    : type === DataType.Boolen
    ? 'boolen'
    : type === 'integer'
    ? 'number'
    : type
}

/**
 *
 * @param str <<值>>
 * @returns 值
 */
export const extractContent = (str: string) =>
  str.match(/«(.*?)»/) && str.match(/«(.*?)»/)!.length > 0 ? str.match(/«(.*?)»/)![1] : undefined
export const removeDoubblerowStr = (str: string, mode: number) => {
  if (mode === 1) {
    return str.indexOf('«') < 0 ? str : str.slice(0, str.indexOf('«'))
  } else {
    return str.indexOf('«') < 0 ? str : str.slice(str.indexOf('«') + 1, str.length)
  }
}
/**
 * 转译数字相关的表达
 */
export const TranslationNumber = (type: string) =>
  type === 'int' ? 'number' : type === 'long' ? 'number' : type